<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>googlevideo.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_ui_media_googlevideo.js.html">googlevideo.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2011 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview provides a reusable GoogleVideo UI component given a public
<a name="line17"></a> * GoogleVideo video URL.
<a name="line18"></a> *
<a name="line19"></a> * goog.ui.media.GoogleVideo is actually a {@link goog.ui.ControlRenderer}, a
<a name="line20"></a> * stateless class - that could/should be used as a Singleton with the static
<a name="line21"></a> * method {@code goog.ui.media.GoogleVideo.getInstance} -, that knows how to
<a name="line22"></a> * render GoogleVideo videos. It is designed to be used with a
<a name="line23"></a> * {@link goog.ui.Control}, which will actually control the media renderer and
<a name="line24"></a> * provide the {@link goog.ui.Component} base. This design guarantees that all
<a name="line25"></a> * different types of medias will behave alike but will look different.
<a name="line26"></a> *
<a name="line27"></a> * goog.ui.media.GoogleVideo expects {@code goog.ui.media.GoogleVideoModel} on
<a name="line28"></a> * {@code goog.ui.Control.getModel} as data models, and renders a flash object
<a name="line29"></a> * that will show the contents of that video.
<a name="line30"></a> *
<a name="line31"></a> * Example of usage:
<a name="line32"></a> *
<a name="line33"></a> * &lt;pre&gt;
<a name="line34"></a> *   var video = goog.ui.media.GoogleVideoModel.newInstance(
<a name="line35"></a> *       &#39;http://video.google.com/videoplay?docid=6698933542780842398&#39;);
<a name="line36"></a> *   goog.ui.media.GoogleVideo.newControl(video).render();
<a name="line37"></a> * &lt;/pre&gt;
<a name="line38"></a> *
<a name="line39"></a> * GoogleVideo medias currently support the following states:
<a name="line40"></a> *
<a name="line41"></a> * &lt;ul&gt;
<a name="line42"></a> *   &lt;li&gt; {@link goog.ui.Component.State.DISABLED}: shows &#39;flash not available&#39;
<a name="line43"></a> *   &lt;li&gt; {@link goog.ui.Component.State.HOVER}: mouse cursor is over the video
<a name="line44"></a> *   &lt;li&gt; {@link goog.ui.Component.State.SELECTED}: flash video is shown
<a name="line45"></a> * &lt;/ul&gt;
<a name="line46"></a> *
<a name="line47"></a> * Which can be accessed by
<a name="line48"></a> * &lt;pre&gt;
<a name="line49"></a> *   video.setEnabled(true);
<a name="line50"></a> *   video.setHighlighted(true);
<a name="line51"></a> *   video.setSelected(true);
<a name="line52"></a> * &lt;/pre&gt;
<a name="line53"></a> *
<a name="line54"></a> *
<a name="line55"></a> * @supported IE6+, FF2+, Chrome, Safari. Requires flash to actually work.
<a name="line56"></a> */
<a name="line57"></a>
<a name="line58"></a>
<a name="line59"></a>goog.provide(&#39;goog.ui.media.GoogleVideo&#39;);
<a name="line60"></a>goog.provide(&#39;goog.ui.media.GoogleVideoModel&#39;);
<a name="line61"></a>
<a name="line62"></a>goog.require(&#39;goog.string&#39;);
<a name="line63"></a>goog.require(&#39;goog.ui.media.FlashObject&#39;);
<a name="line64"></a>goog.require(&#39;goog.ui.media.Media&#39;);
<a name="line65"></a>goog.require(&#39;goog.ui.media.MediaModel&#39;);
<a name="line66"></a>goog.require(&#39;goog.ui.media.MediaModel.Player&#39;);
<a name="line67"></a>goog.require(&#39;goog.ui.media.MediaRenderer&#39;);
<a name="line68"></a>
<a name="line69"></a>
<a name="line70"></a>
<a name="line71"></a>/**
<a name="line72"></a> * Subclasses a goog.ui.media.MediaRenderer to provide a GoogleVideo specific
<a name="line73"></a> * media renderer.
<a name="line74"></a> *
<a name="line75"></a> * This class knows how to parse GoogleVideo URLs, and render the DOM structure
<a name="line76"></a> * of GoogleVideo video players. This class is meant to be used as a singleton
<a name="line77"></a> * static stateless class, that takes {@code goog.ui.media.Media} instances and
<a name="line78"></a> * renders it. It expects {@code goog.ui.media.Media.getModel} to return a well
<a name="line79"></a> * formed, previously constructed, GoogleVideo video id, which is the data model
<a name="line80"></a> * this renderer will use to construct the DOM structure.
<a name="line81"></a> * {@see goog.ui.media.GoogleVideo.newControl} for a example of constructing a
<a name="line82"></a> * control with this renderer.
<a name="line83"></a> *
<a name="line84"></a> * This design is patterned after http://go/closure_control_subclassing
<a name="line85"></a> *
<a name="line86"></a> * It uses {@link goog.ui.media.FlashObject} to embed the flash object.
<a name="line87"></a> *
<a name="line88"></a> * @constructor
<a name="line89"></a> * @extends {goog.ui.media.MediaRenderer}
<a name="line90"></a> */
<a name="line91"></a>goog.ui.media.GoogleVideo = function() {
<a name="line92"></a>  goog.ui.media.MediaRenderer.call(this);
<a name="line93"></a>};
<a name="line94"></a>goog.inherits(goog.ui.media.GoogleVideo, goog.ui.media.MediaRenderer);
<a name="line95"></a>goog.addSingletonGetter(goog.ui.media.GoogleVideo);
<a name="line96"></a>
<a name="line97"></a>
<a name="line98"></a>/**
<a name="line99"></a> * A static convenient method to construct a goog.ui.media.Media control out of
<a name="line100"></a> * a GoogleVideo model. It sets it as the data model goog.ui.media.GoogleVideo
<a name="line101"></a> * renderer uses, sets the states supported by the renderer, and returns a
<a name="line102"></a> * Control that binds everything together. This is what you should be using for
<a name="line103"></a> * constructing GoogleVideo videos, except if you need finer control over the
<a name="line104"></a> * configuration.
<a name="line105"></a> *
<a name="line106"></a> * @param {goog.ui.media.GoogleVideoModel} dataModel The GoogleVideo data model.
<a name="line107"></a> * @param {goog.dom.DomHelper=} opt_domHelper Optional DOM helper, used for
<a name="line108"></a> *     document interaction.
<a name="line109"></a> * @return {goog.ui.media.Media} A Control binded to the GoogleVideo renderer.
<a name="line110"></a> */
<a name="line111"></a>goog.ui.media.GoogleVideo.newControl = function(dataModel, opt_domHelper) {
<a name="line112"></a>  var control = new goog.ui.media.Media(
<a name="line113"></a>      dataModel,
<a name="line114"></a>      goog.ui.media.GoogleVideo.getInstance(),
<a name="line115"></a>      opt_domHelper);
<a name="line116"></a>  // GoogleVideo videos don&#39;t have any thumbnail for now, so we show the
<a name="line117"></a>  // &quot;selected&quot; version of the UI at the start, which is the flash player.
<a name="line118"></a>  control.setSelected(true);
<a name="line119"></a>  return control;
<a name="line120"></a>};
<a name="line121"></a>
<a name="line122"></a>
<a name="line123"></a>/**
<a name="line124"></a> * Default CSS class to be applied to the root element of components rendered
<a name="line125"></a> * by this renderer.
<a name="line126"></a> *
<a name="line127"></a> * @type {string}
<a name="line128"></a> */
<a name="line129"></a>goog.ui.media.GoogleVideo.CSS_CLASS =
<a name="line130"></a>    goog.getCssName(&#39;goog-ui-media-googlevideo&#39;);
<a name="line131"></a>
<a name="line132"></a>
<a name="line133"></a>/**
<a name="line134"></a> * Creates the initial DOM structure of the GoogleVideo video, which is
<a name="line135"></a> * basically a the flash object pointing to a GoogleVideo video player.
<a name="line136"></a> *
<a name="line137"></a> * @param {goog.ui.Control} c The media control.
<a name="line138"></a> * @return {Element} The DOM structure that represents this control.
<a name="line139"></a> * @override
<a name="line140"></a> */
<a name="line141"></a>goog.ui.media.GoogleVideo.prototype.createDom = function(c) {
<a name="line142"></a>  var control = /** @type {goog.ui.media.Media} */ (c);
<a name="line143"></a>  var div = goog.base(this, &#39;createDom&#39;, control);
<a name="line144"></a>
<a name="line145"></a>  var dataModel =
<a name="line146"></a>      /** @type {goog.ui.media.GoogleVideoModel} */ (control.getDataModel());
<a name="line147"></a>
<a name="line148"></a>  var flash = new goog.ui.media.FlashObject(
<a name="line149"></a>      dataModel.getPlayer().getUrl() || &#39;&#39;,
<a name="line150"></a>      control.getDomHelper());
<a name="line151"></a>  flash.render(div);
<a name="line152"></a>
<a name="line153"></a>  return div;
<a name="line154"></a>};
<a name="line155"></a>
<a name="line156"></a>
<a name="line157"></a>/**
<a name="line158"></a> * Returns the CSS class to be applied to the root element of components
<a name="line159"></a> * rendered using this renderer.
<a name="line160"></a> *
<a name="line161"></a> * @return {string} Renderer-specific CSS class.
<a name="line162"></a> * @override
<a name="line163"></a> */
<a name="line164"></a>goog.ui.media.GoogleVideo.prototype.getCssClass = function() {
<a name="line165"></a>  return goog.ui.media.GoogleVideo.CSS_CLASS;
<a name="line166"></a>};
<a name="line167"></a>
<a name="line168"></a>
<a name="line169"></a>
<a name="line170"></a>/**
<a name="line171"></a> * The {@code goog.ui.media.GoogleVideo} media data model. It stores a required
<a name="line172"></a> * {@code videoId} field, sets the GoogleVideo URL, and allows a few optional
<a name="line173"></a> * parameters.
<a name="line174"></a> *
<a name="line175"></a> * @param {string} videoId The GoogleVideo video id.
<a name="line176"></a> * @param {string=} opt_caption An optional caption of the GoogleVideo video.
<a name="line177"></a> * @param {string=} opt_description An optional description of the GoogleVideo
<a name="line178"></a> *     video.
<a name="line179"></a> * @param {boolean=} opt_autoplay Whether to autoplay video.
<a name="line180"></a> * @constructor
<a name="line181"></a> * @extends {goog.ui.media.MediaModel}
<a name="line182"></a> */
<a name="line183"></a>goog.ui.media.GoogleVideoModel = function(videoId, opt_caption, opt_description,
<a name="line184"></a>                                          opt_autoplay) {
<a name="line185"></a>  goog.ui.media.MediaModel.call(
<a name="line186"></a>      this,
<a name="line187"></a>      goog.ui.media.GoogleVideoModel.buildUrl(videoId),
<a name="line188"></a>      opt_caption,
<a name="line189"></a>      opt_description,
<a name="line190"></a>      goog.ui.media.MediaModel.MimeType.FLASH);
<a name="line191"></a>
<a name="line192"></a>  /**
<a name="line193"></a>   * The GoogleVideo video id.
<a name="line194"></a>   * @type {string}
<a name="line195"></a>   * @private
<a name="line196"></a>   */
<a name="line197"></a>  this.videoId_ = videoId;
<a name="line198"></a>
<a name="line199"></a>  this.setPlayer(new goog.ui.media.MediaModel.Player(
<a name="line200"></a>      goog.ui.media.GoogleVideoModel.buildFlashUrl(videoId, opt_autoplay)));
<a name="line201"></a>};
<a name="line202"></a>goog.inherits(goog.ui.media.GoogleVideoModel, goog.ui.media.MediaModel);
<a name="line203"></a>
<a name="line204"></a>
<a name="line205"></a>/**
<a name="line206"></a> * Regular expression used to extract the GoogleVideo video id (docid) out of
<a name="line207"></a> * GoogleVideo URLs.
<a name="line208"></a> *
<a name="line209"></a> * @type {RegExp}
<a name="line210"></a> * @private
<a name="line211"></a> * @const
<a name="line212"></a> */
<a name="line213"></a>goog.ui.media.GoogleVideoModel.MATCHER_ =
<a name="line214"></a>    /^http:\/\/(?:www\.)?video\.google\.com\/videoplay.*[\?#]docid=(-?[0-9]+)#?$/i;
<a name="line215"></a>
<a name="line216"></a>
<a name="line217"></a>/**
<a name="line218"></a> * A auxiliary static method that parses a GoogleVideo URL, extracting the ID of
<a name="line219"></a> * the video, and builds a GoogleVideoModel.
<a name="line220"></a> *
<a name="line221"></a> * @param {string} googleVideoUrl A GoogleVideo video URL.
<a name="line222"></a> * @param {string=} opt_caption An optional caption of the GoogleVideo video.
<a name="line223"></a> * @param {string=} opt_description An optional description of the GoogleVideo
<a name="line224"></a> *     video.
<a name="line225"></a> * @param {boolean=} opt_autoplay Whether to autoplay video.
<a name="line226"></a> * @return {goog.ui.media.GoogleVideoModel} The data model that represents the
<a name="line227"></a> *     GoogleVideo URL.
<a name="line228"></a> * @see goog.ui.media.GoogleVideoModel.getVideoId()
<a name="line229"></a> * @throws Error in case the parsing fails.
<a name="line230"></a> */
<a name="line231"></a>goog.ui.media.GoogleVideoModel.newInstance = function(googleVideoUrl,
<a name="line232"></a>                                                      opt_caption,
<a name="line233"></a>                                                      opt_description,
<a name="line234"></a>                                                      opt_autoplay) {
<a name="line235"></a>  if (goog.ui.media.GoogleVideoModel.MATCHER_.test(googleVideoUrl)) {
<a name="line236"></a>    var data = goog.ui.media.GoogleVideoModel.MATCHER_.exec(googleVideoUrl);
<a name="line237"></a>    return new goog.ui.media.GoogleVideoModel(
<a name="line238"></a>        data[1], opt_caption, opt_description, opt_autoplay);
<a name="line239"></a>  }
<a name="line240"></a>
<a name="line241"></a>  throw Error(&#39;failed to parse video id from GoogleVideo url: &#39; +
<a name="line242"></a>      googleVideoUrl);
<a name="line243"></a>};
<a name="line244"></a>
<a name="line245"></a>
<a name="line246"></a>/**
<a name="line247"></a> * The opposite of {@code goog.ui.media.GoogleVideo.newInstance}: it takes a
<a name="line248"></a> * videoId and returns a GoogleVideo URL.
<a name="line249"></a> *
<a name="line250"></a> * @param {string} videoId The GoogleVideo video ID.
<a name="line251"></a> * @return {string} The GoogleVideo URL.
<a name="line252"></a> */
<a name="line253"></a>goog.ui.media.GoogleVideoModel.buildUrl = function(videoId) {
<a name="line254"></a>  return &#39;http://video.google.com/videoplay?docid=&#39; +
<a name="line255"></a>      goog.string.urlEncode(videoId);
<a name="line256"></a>};
<a name="line257"></a>
<a name="line258"></a>
<a name="line259"></a>/**
<a name="line260"></a> * An auxiliary method that builds URL of the flash movie to be embedded,
<a name="line261"></a> * out of the GoogleVideo video id.
<a name="line262"></a> *
<a name="line263"></a> * @param {string} videoId The GoogleVideo video ID.
<a name="line264"></a> * @param {boolean=} opt_autoplay Whether the flash movie should start playing
<a name="line265"></a> *     as soon as it is shown, or if it should show a &#39;play&#39; button.
<a name="line266"></a> * @return {string} The flash URL to be embedded on the page.
<a name="line267"></a> */
<a name="line268"></a>goog.ui.media.GoogleVideoModel.buildFlashUrl = function(videoId, opt_autoplay) {
<a name="line269"></a>  var autoplay = opt_autoplay ? &#39;&amp;autoplay=1&#39; : &#39;&#39;;
<a name="line270"></a>  return &#39;http://video.google.com/googleplayer.swf?docid=&#39; +
<a name="line271"></a>      goog.string.urlEncode(videoId) +
<a name="line272"></a>      &#39;&amp;hl=en&amp;fs=true&#39; + autoplay;
<a name="line273"></a>};
<a name="line274"></a>
<a name="line275"></a>
<a name="line276"></a>/**
<a name="line277"></a> * Gets the GoogleVideo video id.
<a name="line278"></a> * @return {string} The GoogleVideo video id.
<a name="line279"></a> */
<a name="line280"></a>goog.ui.media.GoogleVideoModel.prototype.getVideoId = function() {
<a name="line281"></a>  return this.videoId_;
<a name="line282"></a>};
</pre>


</body>
</html>
